<?php

namespace App\Http\Controllers\Admin;

use App\Jobs\OpenMemberTask;
use App\Logic\PointLogic;
use App\Models\PointAccount;
use App\Models\UserApplyPointLog;
use App\Standard\CommonHelpers;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Storage;
use function Symfony\Component\Console\Tests\Command\createClosure;
use App\Models\Address;

class IndexController extends Controller
{
    protected function guard()
    {
        return auth()->guard('admin');
    }

    /**
     * 首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.index');
    }

    /**
     * 欢迎页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function welcome()
    {
        return view('admin.welcome');
    }

    /**
     * 文件上传
     * @param Request $request
     * @return string json
     */
//    public function upload(Request $request)
//    {
//        return json_encode(CommonHelpers::upload($request, 'file'));
//    }

    public function couponRecommend()
    {
    }

    /**
     * 显示素材页面
     * @param $type
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function material($type)
    {
        return view('admin.material-' . $type);
    }

    /**
     * 获取各种素材列表的API
     * @param $type
     * @return mixed
     */
    public function materialApi($type)
    {
        $offset = 0;
        $count = 10;
        $app = app('wechat.official_account');
        $list = $app->material->list($type, $offset, $count);
        $list['item'] = WechatMediaMap::format($type, $list['item'])->doing();
        return $list;
    }

    /**
     * 用户列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function userList()
    {
        return view('admin.user-list');
    }

    /**
     * 用户列表api
     * @param Request $request
     * @return array
     */
    public function userListApi(Request $request)
    {
        $perPage = $request->get('per_page', 10);
        $model = User::where([])->paginate($perPage);
        $model->data = $model->makeVisible(['id']);

        return CommonHelpers::responseFormat(0, $model);
    }


    public function sendPoint(Request $request) {
        $userId = $request->get('user_id');
        $user = User::find($userId);
        $this->dispatch(new OpenMemberTask($user));
        $pointAccount = PointAccount::where(['source_id' => $userId, 'type' => PointAccount::TYPE_USER])->first();
        return CommonHelpers::responseFormat(0, $pointAccount);
    }

    public function test()
    {
        $service = \App\Services\hyMallMemberService::create(false);
        $value = config(config('website.app_config').".jf_domain");

        dd($value);
    }

    public function UploadFileToServer(Request $request) {
        return json_encode(CommonHelpers::upload($request, 'file'));
    }



    //判断是否为多账户登录，如果是顶掉之前的用户
    public function UserLogin(Request $request){

        if(session()->getId() !=Redis::get($this->guard()->user()->id)){
            $this->guard()->logout();
            $request->session()->invalidate();
            return response(['code'=>0,'msg'=>'成功']);
        }else{
            return response(['code'=>1,'msg'=>'失败']);
        }
    }
}
